package org.erp.product.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.erp.common.annotation.StockLockBatch;
import org.erp.common.constant.ProductConstant;
import org.erp.common.constant.RabbitmqConstant;
import org.erp.product.domain.dto.AsyncMessageDto;
import org.erp.product.domain.dto.RepairLogAddDto;
import org.erp.product.domain.entity.RepairInfo;
import org.erp.product.domain.entity.RepairLog;
import org.erp.product.domain.entity.TransferLog;
import org.erp.product.infrastructure.config.RepairStateFlow;
import org.erp.product.infrastructure.convert.RepairConvert;
import org.erp.product.infrastructure.enums.RepairEventEnum;
import org.erp.product.infrastructure.enums.RepairStatusEnum;
import org.erp.product.infrastructure.exception.ProductException;
import org.erp.product.infrastructure.mapper.RepairLogMapper;
import org.erp.product.service.RepairInfoService;
import org.erp.product.service.RepairLogService;
import org.erp.product.service.TransferLogService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.UUID;

/**
 * 维修记录服务实现层
 *
 * @author libin
 * @version 1.0
 * @description: 维修记录服务实现层
 * @date 2025/11/13 16:16
 **/
@Log4j2
@Service
@AllArgsConstructor
public class RepairLogServiceImpl extends ServiceImpl<RepairLogMapper, RepairLog> implements RepairLogService {

    private final RepairConvert repairConvert;

    private final TransferLogService transferLogService;

    private final RepairInfoService repairInfoService;

    private final RabbitTemplate rabbitTemplate;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(RepairLogAddDto repairLogAddDto) {
        RepairLog repairLog = repairConvert.dtoConvert(repairLogAddDto);
        RepairInfo productInfo = repairInfoService.getOne(new QueryWrapper<RepairInfo>().lambda()
                .eq(RepairInfo::getStockId, repairLog.getRepairId()));
        try {
            RepairStateFlow.next(RepairStatusEnum.getByCode(productInfo.getRepairStatus()), RepairEventEnum.REPAIR);
        } catch (Exception e) {
            throw new ProductException("产品id为: " + productInfo.getId() + " 状态不允许执行该操作");
        }
        Integer oldStatus = productInfo.getRepairStatus();
        RepairStatusEnum nextStatus = RepairStateFlow.next(RepairStatusEnum.getByCode(productInfo.getRepairStatus()), RepairEventEnum.REPAIR);
        productInfo.setRepairStatus(nextStatus.getCode());
        productInfo.setRepairPrice(productInfo.getRepairPrice() + repairLog.getRepairPrice());
        TransferLog transferLog = TransferLog.buildRepairTransferLog(productInfo, oldStatus);
        this.save(repairLog);
        repairInfoService.updateById(productInfo);
        transferLogService.save(transferLog);
        // 通知仓库维修价格
        AsyncMessageDto asyncMessageDto = new AsyncMessageDto();
        asyncMessageDto.setInventoryId(productInfo.getStockId());
        asyncMessageDto.setRepairPrice(productInfo.getRepairPrice());
        asyncMessageDto.setTransferUserId(productInfo.getOperationId());
        asyncMessageDto.setTransferUserName(productInfo.getOperationName());
        asyncMessageDto.setUuid(UUID.randomUUID().toString());
        rabbitTemplate.convertAndSend(RabbitmqConstant.ERP_STORE_EXCHANGE, RabbitmqConstant.ERP_STORE_UPDATE_ROUTING_KEY, JSONObject.toJSONString(asyncMessageDto));
    }
}
